
package com.xixibaby.core;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;

@Getter
@Setter
public class Response<T> implements Serializable {

    private Boolean status = true;
    private Integer code = ResultCode.SUCCESS.getCode();
    private String message = ResultCode.SUCCESS.getMessage();
    private T data;
    private String sessionId;

    public Response(){}


    Response(ResultCode resultCode){
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = null;
    }

    Response(ResultCode resultCode,T data){
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = data;
    }

    public static <T> Response<T> success(T data) {
        return new Response<T>(ResultCode.SUCCESS, data);
    }


    public static <T,REQUEST extends SuperRequest> Response<T> success(REQUEST request, Function<REQUEST, T> function) {
        return new Response<T>(ResultCode.SUCCESS, function.apply(request));
    }



    public static <T,REQUEST extends SuperRequest,PO extends SuperPO,RES extends SuperResponse> Response<T>
        success(REQUEST request, BiFunction<REQUEST,Function<PO, RES>, T> service, Function<PO, RES> after) {
        return new Response<T>(ResultCode.SUCCESS, service.apply(request,after));
    }

    public static <REQUEST extends SuperRequest,RES extends SuperResponse> Response<RES>
    success(REQUEST request, BiFunction<REQUEST, Consumer<REQUEST>, RES> service, Consumer<REQUEST> valid) {
        return new Response<>(ResultCode.SUCCESS, service.apply(request,valid));
    }


    public static <T> Response<T> failed(T data) {
        return new Response<T>(ResultCode.FAILED, data);
    }

    public static <T> Response<T> failed(String message) {
        Response<T> response = new Response<>(ResultCode.FAILED);
        response.setMessage(message);
        return response;
    }

    public static <T> Response<T> failed(Integer code,String message) {
        Response<T> response = new Response<>(ResultCode.FAILED);
        response.setCode(code);
        response.setMessage(message);
        return response;
    }

//    public static <T> Response<T> success(String message) {
//        Response<T> response = new Response<>(ResultCode.SUCCESS);
//        response.setMessage(message);
//        return response;
//    }

    public static <T> Response<T> failed(ResultCode resultCode) {
        return new Response<T>(resultCode);
    }




    public Response<T> message(String message) {
        this.message = message;
        return this;
    }

    public Response<T> data(T data) {
        this.data = data;
        return this;
    }

}
